package es.ats.codington.web.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class UpdatePasswordServlet
 */
@WebServlet("/UpdatePasswordServlet")
public class UpdatePasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdatePasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_updatePassword.jsp");  
		rd.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		response.setContentType("text/html");   
	    
	    HttpSession session=request.getSession(true);
	    
	    Visitor u =new Visitor();
	    
	    u.setVisitorName((String)session.getAttribute("visitorName"));
	    u.setPassword((String)session.getAttribute("password"));
	    u.setIdVisitor((Integer) session.getAttribute("idVisitor"));
		u.setFirstName((String)session.getAttribute("firstName"));
		u.setLastName((String)session.getAttribute("lastName"));
		u.setEmail((String)session.getAttribute("email"));
		u.setPhone((Long)session.getAttribute("phone"));
		u.setAddress((String)session.getAttribute("address"));
		u.setDni((String)session.getAttribute("dni"));
	    
	    	    
	    VisitorService vs = new VisitorService();
	    
	    try
	    {
	    	
	    	if(request.getParameter("oldpassword").equals(u.getPassword())){
	    		u.setPassword(request.getParameter("password"));
	    		
	    		
	    		if(vs.updateVisitor(u))
	    		{
	    			
		    		session.setAttribute("password",u.getPassword());
		    		
	    			RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortal.jsp");  
	    			rd.forward(request,response);	
	    		}
	    		else{
	    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_updatePassword.jsp");  
    			rd.forward(request,response);
	    		}
	    	}
	    	else
	    	{
	    		session.setAttribute("error", "Incorrect password");
	    		 
	    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_updatePassword.jsp");  
	    		rd.include(request,response);

	    	}
	    	
	    	
	    }
	    catch(Exception eee){
	    	eee.getMessage();	
	    }
		
		
	}

}
